﻿@{
    ViewData["Title"] = "Index";
    Layout = "~/Areas/Console/Views/Shared/_Layout.cshtml";
}
@section head{
    <script type="text/javascript">
        var renderTable;
        var userType = 0;
        layui.config({
            base: '../../layuiadmin/' //静态资源所在路径
        }).extend({
            treetable: 'treetable-lay/treetable',
            index: 'lib/index'
        }).use(['table', 'form', 'treetable', 'index'], function () {
            var $ = layui.$
                , form = layui.form
                , admin = layui.admin
                , table = layui.table
                , element = layui.element
                , treetable = layui.treetable;
            
            element.render();
            element.on('tab(component-tabs-brief)', function (obj) {
                userType = obj.index;
                renderTable();
            });
            // 渲染表格
            renderTable = function () {
                layer.load(2);
                treetable.render({
                    elem: '#table-datalist'
                    , treeColIndex: 0
                    , treeSpid: 0
                    , treeIdName: 'id'
                    , treePidName: 'pid'
                    , url: '/Console/Roles/GetListJson?userType=' + userType
                    , toolbar: '#table-datalist-toolbar'
                    , limit: 20
                    , title: '菜单数据表'
                    , cols: [[
                        { field: 'names', title: '名称', width: 200 }
                        , { field: 'creator', title: '创建人', width: 200 }
                        , { field: 'createDate', title: '添加时间', width: 200 }
                        , { field: 'remark', title: '备注' }
                        , { field: 'operate', fixed: 'right', title: '操作', toolbar: '#table-datalist-bar', width: 300 }
                    ]]
                    , page: { layout: ['prev', 'page', 'next', 'refresh'] }
                    , done: function (res) {
                        if (res.count == 0) {
                            $(".layui-table-main").html('<div class="layui-none">' + top.lang.noData + '</div>');
                        }
                        layer.closeAll('loading');

                        /**多语言初始化 */
                        top.SetListPageLanguage(layui);
                    }
                });
            };
            //加载表格
            renderTable();

            //监听行工具事件
            table.on('tool(table-datalist)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    var postData = {
                        id: data.id
                    };
                    top.ConfirmByKey(layui, 'confirmDelete', postData, "/Console/Roles/Delete", "POST", "deleteSuccess", "deleteFail", function () {
                        renderTable();
                    });
                } else if (obj.event === 'edit') {
                    top.OpenPageByKey(layui, "editRoles", 2, '800px', '500px', '/Console/Roles/Edit?id=' + data.id);
                } else if (obj.event === 'power') {
                    top.OpenPageByKey(layui, "power", 2, '600px', '800px', '/Console/Menus/MenusTree?roleid=' + data.id + '&userType=' + userType);
                } else if (obj.event === 'add') {
                    top.OpenPageByKey(layui, "addNewRoles", 2, '800px', '500px', '/Console/Roles/Create?pid=' + data.id + '&userType=' + userType);
                }
            });

            //头工具栏事件
            table.on('toolbar(table-datalist)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                switch (obj.event) {
                    case 'addNew':
                        top.OpenPageByKey(layui, "addNewRoles", 2, '800px', '500px', '/Console/Roles/Create?userType=' + userType);
                        break;
                };
            });

            form.on('submit(DelItems)', function (data) {
                var checkStatus = table.checkStatus('table-datalist');
                var data = checkStatus.data;
                if (data.length > 0) {
                    var ids = "";
                    for (var i = 0; i < data.length; i++) {
                        if (ids == "")
                            ids = data[i].id;
                        else
                            ids += "," + data[i].id;
                    }
                    if (ids != "") {
                        var postData = {
                            ids: ids
                        };
                        top.ConfirmByKey(layui, 'confirmDelete', postData, "/Console/Roles/DeleteItems", "POST", "deleteSuccess", "deleteFail", function () {
                            renderTable();
                        });
                    }
                }
            });

        });
    </script>
}
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div class="layui-tab layui-tab-brief" lay-filter="component-tabs-brief">
                        <ul class="layui-tab-title">
                            <li lay-event="manageMenus" class="layui-this">管理员</li>
                            <li lay-event="userMenus">用户</li>
                        </ul>
                        <div class="layui-tab-content">
                            <script type="text/html" id="table-datalist-toolbar">
                                <div class="layui-btn-container">
                                    <button class="layui-btn layui-btn-sm" lay-event="addNew">新增</button>
                                </div>
                            </script>
                            <table class="layui-hide" id="table-datalist" lay-filter="table-datalist"></table>
                            <script type="text/html" id="table-datalist-bar">
                                <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="edit">编辑</button>
                                <button class="layui-btn layui-btn-sm layui-btn-warm" lay-event="power">权限分配</button>
                                <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</button>
                            </script>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>